package ocs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class MainWindowController {

    private MainWindowView view;

    private AccountHelper accountHelper;

    private Account googleAccount;

    private Account ovsAccount;

    public MainWindowController(MainWindowView view) throws FileNotFoundException, IOException {
        this.view = view;
        this.view.setController(this);
        this.accountHelper = new AccountHelper("cookies.properties");
        this.googleAccount = accountHelper.getGoogleAccount();
        this.ovsAccount = accountHelper.getOvsAccount();
    }

    public void start() {
        view.setOvsAccount(ovsAccount);
        view.setGoogleAccount(googleAccount);
        view.display();
        try {
            synchronize();
        }
        catch (Exception e) {

        }
        // TODO Auto-generated catch block

    }

    public void synchronize() throws Exception {

        Account ovsAccount = getOvsAccount();
        Account gooAccount = getGoogleAccount();

        OvsAgendaReader ovsAgenda = new OvsAgendaReader();
        ovsAgenda.loadData(ovsAccount);
        List<OvsEvent> eventList = ovsAgenda.getEventList();

        GoogleAgendaWriter googleAgenda = new GoogleAgendaWriter(gooAccount);
        googleAgenda.addEvents(eventList);

    }

    public void saveAccounts() throws IOException {
        accountHelper.saveAccounts(googleAccount, ovsAccount);
    }

    private Account getGoogleAccount() {
        String login = view.getGoogleLogin();
        String password = view.getGooglePassword();
        String authToken = accountHelper.getGoogleAuthToken();
        return new Account(login, password, authToken);
    }

    private Account getOvsAccount() {
        String login = view.getOvsLogin();
        String password = view.getOvsPassword();
        String authToken = accountHelper.getOvsAuthToken();
        return new Account(login, password, authToken);
    }

    public boolean isOvsAccountMemorized() {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean isGoogleAccountMemorized() {
        // TODO Auto-generated method stub
        return false;
    }
}
